package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.glowmee.dao.AdminTableDAO;

public class PushBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
    public PushBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/push.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		String pushText = request.getParameter("pushText");
		
		try {
			if (pushText != null && !pushText.equals("")) {
				int send = AdminTableDAO.getInstance().sendPush(pushText);
				if (send > 0) {
					request.setAttribute("alert", "<script type='text/javascript'>alert('푸쉬가 발송되었습니다.')</script>");
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/push.jsp");
		dpr.forward(request, response);
	}
	
}
